﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/InternalPage.Master"
  Inherits="System.Web.Mvc.ViewPage<IEnumerable<GiveAQuiz.Data.QuizDefinition>>"  %>
<%@ Import Namespace="GiveAQuiz.Common" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  Your Quizzes
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" runat="server">
  Your Quizzes
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
  <div style="width: 100%">
    <% foreach ( var item in Model )
       { %>
    <div class="section">
      <div class="inline">
        <h3>
          <a href="#" class="section-header"><span class="ui-icon ui-icon-triangle-1-s" style="float: left;
            margin: 3px 3px;"></span>
            <%: item.QuizName %>
          </a>
        </h3>
      </div>
      <div class="quiz-row">
        <%= Html.ActionLink("Start Quiz", "Start", new { quizDefinitionId = item.Id }, new { @class = "quiz-action" }) %>
        <%= item.CanEdit() ? " | " + Html.ActionLink("Edit Quiz Def", "Edit", new { id = item.Id }, new { @class = "quiz-action" }).ToHtmlString() : String.Empty %>
        | <a href="#" onclick="javascript:deleteQuizDefinition(<%= item.Id %>);" class="quiz-action">
          Delete Quiz Def</a>
      </div>
      <div class="clearDiv">
      </div>
      <div class="content">
        <div class="clearDiv">
        </div>
        <% item.Quizzes.Each(quiz =>
                     {
        %>
        <div class="section" style="padding: 15px 3px 5px 20px;">
          <div class="inline">
            <h3>
              <a href="#" class="section-header"><span class="ui-icon ui-icon-triangle-1-e" style="float: left;
                margin: 3px 3px;"></span>
                <%: quiz.Name %>
              </a>
            </h3>
          </div>
          <div class="quiz-row">
            <a href="#quiz-menu-<%= quiz.Id %>" class="fg-button fg-button-icon-right ui-state-default ui-widget ui-corner-all">
              <span class='ui-icon ui-icon-triangle-1-s'></span>Action</a>
            <div id="quiz-menu-<%= quiz.Id %>" class="menu-hidden">
              <ul>
                <%= !quiz.HasEnded ? String.Format("<li><a href=\"#\" onclick=\"window.location='{0}'\">Take Quiz</a></li>", Url.Action("Pick", "Quiz", new { passcode = quiz.Password })) : String.Empty%>
                <%= quiz.CanRestart() ? String.Format("<li><a href=\"#\" onclick=\"window.location='{0}'\">Start Quiz</a></li>", Url.Action("Start", "Quiz", new { id = quiz.Id })) : String.Empty%>
                <%= quiz.CanClearResults() ? 
              String.Format("<li><a href=\"#\" onclick=\"window.location = '{0}'\">Run Report</a></li><li><a href=\"#\" onclick=\"javascript:clearQuizResults({1})\">Clear Results</a></li>", Url.Action("QuizResults", "Report", new { quizId = quiz.Id }), quiz.Id) : String.Empty %>
                <%= quiz.CanEnd() ? String.Format("<li><a href=\"#\" onclick=\"javascript:endQuiz({0})\">End Quiz</a></li>", quiz.Id) : String.Empty%>
                <li><a href="#" onclick="javascript:deleteQuiz(<%= quiz.Id  %>)">Delete Quiz</a></li>
              </ul>
            </div>
          </div>
          <div class="clearDiv">
          </div>
          <div class="content center" style="width: 100%; margin: 0px 5px 0px 50px; display: none">
            <div class="inline">
              Start Date:
              <%: quiz.StartDate.ToString("MM/dd/yyyy") %>
            </div>
            <div class="inline" style="margin-left: 15px;">
              End Date:
              <%: quiz.EndDate.ToString("MM/dd/yyyy") %>
            </div>
            <div class="clearDiv">
            </div>
            <div>
              <table class="grid" border="0" cellpadding="0" cellspacing="0" style="width: 450px;">
                <thead>
                  <tr>
                    <th class="ui-state-default ui-th-column" style="width: 50%;">
                      Student
                    </th>
                    <th class="ui-state-default ui-th-column" style="width: 20%;">
                      #/Correct
                    </th>
                    <th class="ui-state-default ui-th-column" style="width: 20%;">
                      Percentage
                    </th>
                    <th class="ui-state-default ui-th-column" style="width: 10%;">
                      &nbsp;
                    </th>
                  </tr>
                </thead>
                <tbody>
                  <% if ( quiz.QuizResults == null || quiz.QuizResults.Count == 0 )
                     {
                  %>
                  <tr>
                    <td colspan="4" style="text-align: left">
                      No results for this quiz
                    </td>
                  </tr>
                  <%
                    }
                     else
                     {%>
                  <%
                    quiz.QuizResults.Each(qr =>
                                            {
                                              // Calculate textual result for the test taker
                                              var total = qr.Quiz.QuizDefinition.QuestionDefinitions.Count();
                                              var right = qr.CorrectAnswers();
                                              var inconclusive = qr.InconclusiveAnswers();
                                              var percent = total - inconclusive == 0 ? 0 : Convert.ToInt32(((Convert.ToDouble(right) / (total - inconclusive)) * 100));
                  %>
                  <tr>
                    <td>
                      <%:qr.QuizTaker.Name%>
                    </td>
                    <td>
                      <%: String.Format("{0} / {1}", right, total - inconclusive) %>
                    </td>
                    <td>
                      <%: String.Format("{0}%", percent) %>
                    </td>
                    <td>
                      <%= Html.ActionLink("Details", "QuizDetails", "Report", new { quizResultsId = qr.Id }, null)%>
                    </td>
                  </tr>
                  <%
                    });
                     }%>
                </tbody>
              </table>
            </div>
          </div>
        </div>
        <%
          }); %>
      </div>
    </div>
    <hr style="width: 550px;" />
    <% } %>
    <form id="deleteQuizDefinitionForm" action="<%= Url.Action("DeleteDefinition", "Quiz") %>"
    method="post">
    <input type="hidden" name="id" id="deleteQuizDefinitionId" />
    </form>
    <form id="deleteQuizForm" action="<%= Url.Action("Delete", "Quiz") %>" method="post">
    <input type="hidden" name="id" id="deleteQuizId" />
    </form>
    <form id="endQuizForm" action="<%= Url.Action("End", "Quiz") %>" method="post">
    <input type="hidden" name="id" id="endQuizId" />
    </form>
    <form id="clearQuizResultsForm" action="<%= Url.Action("ClearResults", "Quiz") %>"
    method="post">
    <input type="hidden" name="id" id="clearQuizResultsId" />
    </form>
    <div class="center quiz-buttons">
      <%= Html.ActionLink("Cancel", "Index", "Home", null, new { @class = "btn" })%>
      <%= Html.ActionLink("New Quiz", "Create", null, new { @class = "btn" })%>
    </div>
  </div>
</asp:Content>
<asp:Content ID="quizzesScripts" ContentPlaceHolderID="Scripts" runat="server">
  <%= Html.ScriptInclude("~/Scripts/fg.menu.js") %>
  <script type="text/javascript">
    $(document).ready(function () {
      resetMenu();
    });
  </script>
</asp:Content>
